package com.fireline.beauty.cashier;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;

import com.fireline.beauty.jdbc.SqliteJdbcImpl;
import com.fireline.beauty.member.HyjfEdit;
import com.fireline.beauty.member.TcxfEdit;
import com.zgw.fireline.base.controls.DataTable;
import com.zgw.fireline.base.controls.DataTableColumn;
import com.zgw.fireline.base.dataset.Dataset;
import com.zgw.fireline.base.frame.FunctionDialog;
import com.zgw.fireline.base.frame.IFunction;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.springframework.beans.factory.config.BeanPostProcessor;

/**
 * 会员交费视图
 * */
public class HyjfView implements IFunction {
	/**
	 * @wbp.nonvisual location=27,492
	 */
	private final Dataset hyjfSet = new Dataset(
			"select * from hyjf where hybh=?\r\nand (?='所有类型' or ztsm =? )",
			SqliteJdbcImpl.INSTANCE);

	private String hybh;
	private Combo combo;

	private Shell shell;

	/**
	 * @wbp.parser.entryPoint
	 */
	@Override
	public Control createControl(Composite parent) {
		this.shell = parent.getShell();
		hyjfSet.setKeyColumns(new String[] {});
		Composite content = new Composite(parent, SWT.NONE);
		content.setLayout(new GridLayout(3, false));

		Label lblNewLabel = new Label(content, SWT.NONE);
		lblNewLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		lblNewLabel.setText("交费类型：");

		combo = new Combo(content, SWT.NONE);
		combo.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				setParams();
				hyjfSet.update();
			}
		});
		combo.setItems(new String[] { "所有类型", "储值卡交费", "套餐续费" });
		combo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1,
				1));
		combo.select(0);

		ToolBar toolBar = new ToolBar(content, SWT.FLAT | SWT.RIGHT);
		GridData gd_toolBar = new GridData(SWT.LEFT, SWT.CENTER, true, false,
				1, 1);
		gd_toolBar.horizontalIndent = 50;
		toolBar.setLayoutData(gd_toolBar);

		ToolItem tltmNewItem = new ToolItem(toolBar, SWT.NONE);
		tltmNewItem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doHyjf();
			}
		});
		tltmNewItem.setText("会员交费");

		ToolItem tltmNewItem_1 = new ToolItem(toolBar, SWT.NONE);
		tltmNewItem_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doTcxf();
			}
		});
		tltmNewItem_1.setText("套餐续费");

		ToolItem tltmNewItem_2 = new ToolItem(toolBar, SWT.NONE);
		tltmNewItem_2.setText("打印");

		ToolItem tltmNewItem_3 = new ToolItem(toolBar, SWT.NONE);
		tltmNewItem_3.setText("导出");

		DataTable dataTable = new DataTable(content, SWT.BORDER
				| SWT.FULL_SELECTION, hyjfSet);
		dataTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 3,
				1));
		dataTable.setHeaderVisible(true);
		dataTable.setLinesVisible(true);

		DataTableColumn dataTableColumn = new DataTableColumn(dataTable,
				SWT.NONE, "rq");
		dataTableColumn.setText("日期");
		dataTableColumn.setWidth(100);

		DataTableColumn dataTableColumn_1 = new DataTableColumn(dataTable,
				SWT.NONE, "ztsm");
		dataTableColumn_1.setText("交费类型");
		dataTableColumn_1.setWidth(80);

		DataTableColumn dataTableColumn_2 = new DataTableColumn(dataTable,
				SWT.NONE, "yfje");
		dataTableColumn_2.setText("应付金额");
		dataTableColumn_2.setWidth(80);

		DataTableColumn dataTableColumn_3 = new DataTableColumn(dataTable,
				SWT.NONE, "jfje");
		dataTableColumn_3.setText("实付金额");
		dataTableColumn_3.setWidth(80);

		DataTableColumn dataTableColumn_4 = new DataTableColumn(dataTable,
				SWT.NONE, "yhje");
		dataTableColumn_4.setText("赠送金额");
		dataTableColumn_4.setWidth(80);

		DataTableColumn dataTableColumn_5 = new DataTableColumn(dataTable,
				SWT.NONE, "sjje");
		dataTableColumn_5.setText("实际到帐");
		dataTableColumn_5.setWidth(80);

		DataTableColumn dataTableColumn_6 = new DataTableColumn(dataTable,
				SWT.NONE, "fkyh");
		dataTableColumn_6.setText("优惠金额");
		dataTableColumn_6.setWidth(80);

		DataTableColumn dataTableColumn_7 = new DataTableColumn(dataTable,
				SWT.NONE, "fkqk");
		dataTableColumn_7.setText("欠款金额");
		dataTableColumn_7.setWidth(80);

		DataTableColumn dataTableColumn_8 = new DataTableColumn(dataTable,
				SWT.NONE, "xj");
		dataTableColumn_8.setText("现金");
		dataTableColumn_8.setWidth(80);

		DataTableColumn dataTableColumn_9 = new DataTableColumn(dataTable,
				SWT.NONE, "xyk");
		dataTableColumn_9.setText("信用卡");
		dataTableColumn_9.setWidth(80);

		DataTableColumn dataTableColumn_10 = new DataTableColumn(dataTable,
				SWT.NONE, "djq");
		dataTableColumn_10.setText("代金券");
		dataTableColumn_10.setWidth(80);

		DataTableColumn dataTableColumn_11 = new DataTableColumn(dataTable,
				SWT.NONE, "czk");
		dataTableColumn_11.setText("储值卡");
		dataTableColumn_11.setWidth(80);

		DataTableColumn dataTableColumn_12 = new DataTableColumn(dataTable,
				SWT.NONE, "czy");
		dataTableColumn_12.setText("操作人");
		dataTableColumn_12.setWidth(80);

		DataTableColumn dataTableColumn_13 = new DataTableColumn(dataTable,
				SWT.NONE, "bz");
		dataTableColumn_13.setText("备注");
		dataTableColumn_13.setWidth(80);
		new Label(content, SWT.NONE);
		new Label(content, SWT.NONE);
		new Label(content, SWT.NONE);
		setParams();
		return content;
	}

	protected void doTcxf() {
		TcxfEdit tcxf = new TcxfEdit(hybh);
		FunctionDialog dlg = new FunctionDialog(shell, tcxf);
		dlg.open();
		if (tcxf.result == SWT.OK) {
			refresh(hybh);
			fireChange();
		}
	}

	protected void doHyjf() {
		HyjfEdit hyjf = new HyjfEdit(hybh);
		FunctionDialog dlg = new FunctionDialog(shell, hyjf);
		dlg.open();
		if (hyjf.result == SWT.OK) {
			refresh(hybh);
			fireChange();
		}
	}

	public void refresh(String hybh) {
		this.hybh = hybh;
		setParams();
		hyjfSet.update();
	}

	protected void setParams() {
		hyjfSet.setParam(0, hybh);
		hyjfSet.setParam(1, combo.getText());
		hyjfSet.setParam(2, combo.getText());
	}

	private Listener changeListener;

	/**
	 * <p>
	 * <b> 会员费用和套餐信息改变监听</b>
	 * </p>
	 * 当执行会员充值或套餐续费动作成功后，触发此事件
	 * 
	 * @param changeListener
	 * */
	public void setChangeListener(Listener changeListener) {
		this.changeListener = changeListener;
	}

	private void fireChange() {
		if (changeListener != null) {
			Event event = new Event();
			event.data = hybh;
			changeListener.handleEvent(event);
		}
	}
}
